package com.pioneer.controller.train;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.train.domain.TrainNews;
import com.pioneer.web.train.service.ITrainNewsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * Title:TrainNewsController
 * Description:
 *
 * @Author 罗熠轩
 * @Date 25/2/2025 AM10:18
 * @Version 1.0
 **/


@RestController
@RequestMapping("/train/news")
public class TrainNewsController extends BaseController {

    @Resource
    private ITrainNewsService newsService;

    /**
     * 获取培训类别列表
     *
     * @param news 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('train:news:list')")
    @GetMapping("/list")
    public AjaxResult list(TrainNews news) {
        boolean isPage = startPage();
        List<TrainNews> list = newsService.selectList(news);
        return isPage ? getDataTable(list) : AjaxResult.success(list);
    }

    /**
     * 根据培训类别id获取详细信息
     *
     * @param newsId 培训类别id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('train:news:query')")
    @GetMapping(value = "/{newsId}")
    public AjaxResult getInfo(@PathVariable Long newsId) {
        return AjaxResult.success(newsService.getById(newsId));
    }

    /**
     * 新增培训类别
     *
     * @param news 培训类别
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('train:news:add')")
    @Log(title = "培训类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TrainNews news) {
        news.setCreateBy(getUsername());
        return toAjax(newsService.save(news));
    }

    /**
     * 修改培训类别
     *
     * @param news 培训类别
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('train:news:edit')")
    @Log(title = "培训类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TrainNews news) {
        news.setUpdateBy(getUsername());
        return toAjax(newsService.updateById(news));
    }

    /**
     * 删除培训类别
     *
     * @param newsIds 培训类别id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('train:news:remove')")
    @Log(title = "培训类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{newsIds}")
    public AjaxResult remove(@PathVariable Long[] newsIds) {
        return toAjax(newsService.removeByIds(Arrays.asList(newsIds)));
    }
}
